import 'package:flutter/material.dart';
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
import 'dart:async';
class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  String userInfo;
  int _defaultIndex = 0;
  List<BottomItem> bottomItems;
  List<BottomNavigationBarItem> barItems;

  @override
  void initState() {
    super.initState();
    bottomItems=[
      BottomItem(title: "首页",icon: Icon(Icons.home),url: "http://106.12.180.103/flutter01.html",isOffset: false),
        //BottomItem(title: "首页",icon: Icon(Icons.home),url: "https://www.dzsbk.cn/smartDemo/page/main/index.html",isOffset: false),
        //BottomItem(title: "发现",icon: Icon(Icons.find_in_page),url: "https://www.baidu.com"),
      BottomItem(title: "医疗",icon: Icon(Icons.insert_invitation),url: "https://www.so.com"),
      //BottomItem(title: "我的",icon: Icon(Icons.person),url: "https://www.dzsbk.cn/smartDemo/page/user/index.html"),
      //BottomItem(title: "客服",icon: Icon(Icons.contacts),url: "https://v4.faqrobot.net/robot/h5chat_WD.html?sysNum=1504143616767525"),
    ];
    barItems = bottomItems.map((item) {
      return BottomNavigationBarItem(
        icon: item.icon,
        title: Text(item.title),

      );
    }).toList();
  }

  //底部选项卡事件
  _onTapBottomItems(index) {
    if (_defaultIndex == index) return;
    setState(() {
      _defaultIndex = index;
      bottomItems.forEach((item) {
        if (bottomItems[index] == item && item.isOffset == true)
          item.isOffset = false;
        else
          item.isOffset = true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    SafeArea safeArea = SafeArea(
      child: new Scaffold(
        body: Stack(
          children: bottomItems.map((item) {
            return SafeArea(
              child: Offstage(
                offstage: item.isOffset,
                child: WillPopScope(
                    key: UniqueKey(),
                    child: InAppWebView(
                      key: GlobalObjectKey(item),
                      initialUrl: item.url,
                      onWebViewCreated: (
                          InAppWebViewController webViewController) {
                        item.webViewController = webViewController;
                        //item.webViewController.reload();
                      },
                      onLoadStop: ( InAppWebViewController webViewController,String url) async{
                        print("url:$url");
                        print(await webViewController.injectScriptCode("callJS()"));
                        webViewController.addJavaScriptHandler("handlerFoo", (args){
                              return args;
                        });
                      },
                    ),
                    onWillPop: () async {
                      var canGoback = await item.webViewController.canGoBack();
                      if (canGoback) {
                        await item.webViewController.goBack();
                        return Future.value(false);
                      }
                      return true;
                    }),
              ),
            );
          }).toList(),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: barItems,
          onTap: _onTapBottomItems,
          currentIndex: _defaultIndex,
          type: BottomNavigationBarType.fixed,
        ),
      ),
    );
    return safeArea;
  }

  @override
  void dispose() {
    print("logout!!!!!!!!!!!");
    super.dispose();
  }
}

class BottomItem{
  final String title;
  final Icon icon;
   bool isOffset;
  final String url;
   Completer<InAppWebViewController> controller;
    InAppWebViewController webViewController;
  BottomItem({this.title,this.icon,this.url,this.isOffset=true});
}